﻿<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>更改背景</title>
    <link href="/static/css/main-page/g-reset.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" href="/static/js/layui/css/layui.css"/>
    <script src="/static/js/layui/layui.js"></script>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
</head>
<style>
    html,body{
        height: 100%;
        width: 100%;
    }
    .area-title{
        line-height:36px;
        background:rgba(243,247,255,1);
        border-radius:4px;
        border:1px solid rgba(56,107,231,0.29);
        font-size:14px;
        font-family:Microsoft YaHei-Bold,Microsoft YaHei;
        font-weight:bold;
        color:#303133;
        margin: 16px 8px 0;
        padding-left: 16px;
    }
    .area-title .tip{
        font-size:12px;
        font-family:Microsoft YaHei;
        color:rgba(74,137,253,1);
        text-indent: 5px;
    }
    .default-bg>li,.custom-bg>li{
        float: left;
        overflow: hidden;
        margin: 1% 1.97% 0;
        text-align: center;
        position: relative;
    }
    .default-bg>li span,.custom-bg>li span{
        display: none;
    }
    li.bg-checked span{
        display: block;
        position: absolute;
        top: 0;
        right: 0;
        width: 25px;
        height: 25px;
        background: #0b80ef;
        line-height: 25px;
        text-align: center;
        color: #fff;
    }
    .default-bg>li img,.custom-bg>li img{
        border-radius: 5px;
        border:1px solid rgba(151,151,151,1);
    }
    .add-bg>div{
        height: 90px;
        width: 160px;
        border: dashed 2px #888888;
        border-radius: 5px;
        line-height: 90px;
        font-size: 50px;
        text-align: center;
        color: #888888;
        cursor: pointer;
    }
    .layui-upload-img{
        width: 160px;
        height: 90px;
        border: 1px solid #dfdfdf;
    }

    .layui-upload-list{
        position: relative;
    }
    #demoText{
        position: absolute;
        width: 100%;
        text-align: center;
        line-height: 20px;
        bottom: 0;
    }

    #test1{
        display: inline-block;
        height: 90px;
        width: 90px;
        border: dashed 2px #888888;
        border-radius: 5px;
        line-height: 90px;
        font-size: 50px;
        text-align: center;
        color: #888888;
        font-family:Arial;
        vertical-align: middle;
        box-sizing: border-box;
        cursor: pointer;
    }
    #test3 {
        padding: 0 30px;
        background: #004BFF;
        background: -webkit-linear-gradient(135deg,#004BFF 0%,#8955FF 100%);
    }
</style>
<body>
<div class="mini-fit" style="border:1px solid #dfdfdf">
    <div class="area-title">推荐背景 <span class="tip">点击图片即可直接切换皮肤，切换后，设置自动保存。</span></div>
    <ul class="default-bg clearfix">
    </ul>
    <div class="area-title">我的背景<span class="tip"> &nbsp;Tips:右键删除背景</span></div>
    <ul class="custom-bg clearfix">
    </ul>
</div>

<div id="uploadWindow" class="mini-window" title="文件上传" style="width:400px;height: 300px;" showModal="false" allowResize="true" allowDrag="true">
    <div class="mini-fit">
        <div class="layui-inline" style="display:inline-block;width: 100%;text-align: center;margin: 10px 0;">
            <label class="layui-form-label" style="float: none; display: inline-block;width: 90px;box-sizing: border-box;">主题名称：</label>
            <div class="layui-input-inline" style="width:157px;">
                <input type="text" name="bgName" autocomplete="off" id="bgName" class="layui-input" style="padding: 0;text-indent: 12px;">
            </div>
        </div>
        <div class="layui-upload" style="text-align: center">
            <div id="test1">+</div>
            <div class="layui-upload-list" style="display: inline-block;">
                <img class="layui-upload-img" id="demo1" src="/static/images/data-empty.png"/>
                <p id="demoText"></p>
            </div>
            <div style="text-align: center;padding-top: 20px; "><button type="button" class="layui-btn" id="test3">上传</button></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();

    var uploadWindow = mini.get("uploadWindow");
    $('.custom-bg').on('click','.add-bg',function(){
        uploadWindow.show()
    });

    //JavaScript代码区域
    layui.use('element', function(){
        var element = layui.element;

    });


    var UPLOAD_FILES;
    layui.use(['form','upload'], function() {
        var form = layui.form;
        var $ = layui.jquery
            , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/azbj/background/uploadFile' //改成您自己的上传接口
            ,auto: false
            ,bindAction: '#test3'
            ,data:{
                backgroundName:function(){
                    return $('#bgName').val();
                }
            }
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                UPLOAD_FILES = obj.pushFile();
                clearFile();    //将所有文件先删除再说
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                    obj.pushFile(); //再把当前文件重新加入队列
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 1){
                    return layer.msg('上传失败');
                }else{
                    getBgList();
                    $('#bgName').val('');
                    $('#demo1').attr('src','/static/images/data-empty.png');
                    clearFile()
                    uploadWindow.hide();
                    return layer.msg('上传成功');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

    });

    //清空文件队列
    function clearFile(){
        for (var x in UPLOAD_FILES) {
            delete UPLOAD_FILES[x];
        }
    }

    function getBgList(){
        $.ajax({
            url:'/azbj/background/getBackgroundData',
            type:'GET',
            dataType:'json',
            success:function(data){
                var data=data.data;
                var defaultBgList='',customBgList='<li class="add-bg"><div>+</div></li>';
                for(var i=0,len=data.length;i<len;i++){
                    if(data[i].isDefault=="1"){
                        if(data[i].isUsed=="1"){
                            defaultBgList+='<li  class="bg-checked">';
                        }else{
                            defaultBgList+='<li>';
                        }
                        defaultBgList+='<img data-src="'+data[i].serverUrl+'" data-id="'+data[i].id+'" ' +
                            'src="/static/images/'+data[i].serverUrl+'" alt=""  width="160px" height="90px"/>';
                        defaultBgList+='<span><i class="fa fa fa-check-square-o"></i></span>';
                        defaultBgList+='<p style="line-height: 25px;height: 25px;width: 160px">'+data[i].backgroundName+'</p>';
                        defaultBgList+='</li>';
                    }else{
                        if(data[i].isUsed=="1"){
                            customBgList+='<li  class="bg-checked">';
                        }else{
                            customBgList+='<li>';
                        }
                        customBgList+='<img data-src="'+data[i].serverUrl+'" data-id="'+data[i].id+'" src="/azbj/background/readImage?url='+data[i].serverUrl+'" alt=""  width="160px" height="90px"/>';
                        customBgList+='<span><i class="fa fa fa-check-square-o"></i></span>';
                        customBgList+='<p style="line-height: 25px;height: 25px;width: 160px">'+data[i].backgroundName+'</p>';
                        customBgList+='</li>';
                    }
                }
                $('.default-bg').html(defaultBgList);
                $('.custom-bg').html(customBgList);
            }
        })
    }
    $(function(){
        getBgList();
        // 点击图片设置背景
        $('.default-bg,.custom-bg').on('click','img',function(){
            var bgId=$(this).data('id');
            if(!$(this).parent('li').hasClass('bg-checked')){
                $.ajax({
                    url:'/azbj/background/setBackground',
                    type:'POST',
                    dataType:'json',
                    data:{id:bgId},
                    success:function(data){
                        window.parent.setBg();
                        getBgList();
                        mini.showTips({
                            content: "<b>成功</b> <br/>背景图设置成功",
                            state: 'success',
                            x: 'Center',
                            y: 'Top',
                            timeout: 3000
                        });
                    }
                })
            }
        });

        //删除我的背景
        $('.default-bg,.custom-bg').on('contextmenu','img',function(ev){
            ev.preventDefault();
            var bgId=$(this).data('id');
            var bgSrc=$(this).data('src');
            var ev=ev||window.event;
            if($(this).parent('li').hasClass('bg-checked')){
                mini.showTips({
                    content: "<b>提示</b> <br/>正在使用背景不可删除！",
                    state: 'danger',
                    x: 'Center',
                    y: 'Top',
                    timeout: 3000
                });
            }else if($(this).parents('ul').hasClass('default-bg')){
                mini.showTips({
                    content: "<b>提示</b> <br/>推荐背景不可删除！",
                    state: 'danger',
                    x: 'Center',
                    y: 'Top',
                    timeout: 3000
                });
            }else {
                mini.confirm("确定删除记录？", "确定？",
                    function (action) {
                        if (action == "ok") {
                            $.ajax({
                                url:'/azbj/background/deleteUploadFile',
                                type:'POST',
                                dataType:'json',
                                data:{id:bgId,fileUrl:bgSrc},
                                success:function(data){
                                    getBgList();
                                }
                            })
                        } else {

                        }
                    }
                )
            }
        })

    })

</script>
</body>
</html>